﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;
using System.Collections;

namespace Web.Information
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!((Account)Session["Account"]).RoleCurrent.Equals('s')) Response.Redirect("Default.aspx");
            //初始化学生计算机能力信息
            Student myStudent = (Student)Session["Student"];
            if (myStudent.StudentName == "")
                Response.Redirect("StuInformation.aspx");
            GridView_Computer.DataSource = myStudent.Computers.GetDataSet();
            GridView_Computer.DataBind();

            //绑定DropDownList数据
            if (!IsPostBack)
            {
                ArrayList arraylist = new ArrayList();
                arraylist = Library.GetComputerLib();
                for (int i = 0; i < arraylist.Count; i++)
                {
                    ListItem myListItem = new ListItem();
                    myListItem = (ListItem)arraylist[i];
                    DropDownList_computerlevel.Items.Add(myListItem);
                }
                ListItem otherListItem=new ListItem();
                otherListItem.Text="其它";
                otherListItem.Value="others";
                DropDownList_computerlevel.Items.Add(otherListItem);     
            }
            if (DropDownList_computerlevel.SelectedValue == "others")
            {
                Label_nolist.Visible = true;
                TextBox_nolist.Visible = true;
                RequiredFieldValidator_nolist.Enabled = true;
            }
            else
            {
                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                RequiredFieldValidator_nolist.Enabled = false;
            }
           
            
            #region ViewState

            //初始化ViewState       
            if (ViewState["status"] == null)
                ViewState["status"] = 1;

            this.ViewStateControls(int.Parse(ViewState["status"].ToString()));

            #endregion ViewState
        }
        protected void ViewStateControls(int i)
        {
            if (i == 1)//添加
            {
                ImageButton_Cancel.Visible = false;
                ImageButton_Add.Visible = true;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 2)//修改
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = true;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 3) //删除
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = true;
            }
        }
        
        //GridView的修改按钮
        protected void GridViewButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            Computer myComputer = new Computer();
            Student myStudent = (Student)Session["Student"];
            myComputer = myStudent.Computers.GetOne(decimal.Parse(((ImageButton)sender).CommandArgument));
            Label_ComputerID.Text = myComputer.id.ToString();
            TextBox_computertime.Text = ((DateTime)myComputer.computer_time).ToShortDateString();

            ViewState["status"] = 2;
            Label_result.Text = "";
            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = myComputer.computer_level +" "+ ((DateTime)myComputer.computer_time).ToShortDateString();
        }
        
        //GridView的删除按钮
        protected void GridViewButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            Computer myComputer = new Computer();
            Student myStudent = (Student)Session["Student"];
            myComputer = myStudent.Computers.GetOne(decimal.Parse(((ImageButton)sender).CommandArgument));
            Label_ComputerID.Text = myComputer.id.ToString();
            TextBox_computertime.Text = ((DateTime)myComputer.computer_time).ToShortDateString();

            ViewState["status"] = 3;
            Label_result.Text = "";
            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = myComputer.computer_level + " "+((DateTime)myComputer.computer_time).ToShortDateString();
        }

        //修改按钮
        protected void ImageButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            Computer myComputer = new Computer();
            Student myStudent1 = (Student)Session["Student"];
            myComputer.id = decimal.Parse(Label_ComputerID.Text.ToString());
            if (TextBox_nolist.Text == null || TextBox_nolist.Text == string.Empty)
                myComputer.computer_level = DropDownList_computerlevel.SelectedValue;
            else
            {
                myComputer.computer_level = TextBox_nolist.Text.ToString();
            }
            try
            {
                myComputer.computer_time = (DateTime.Parse(TextBox_computertime.Text.ToString()));
                string str = myStudent1.Computers.Update(myComputer);

                if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
                {
                    Label_result.Text = Resources.ErrorMessage.OK;
                    Student myStudent2 = (Student)Session["Student"];
                    GridView_Computer.DataSource = myStudent2.Computers.GetDataSet();
                    GridView_Computer.DataBind();
                }
                else Label_result.Text = Resources.ErrorMessage.DBOptError;

                ViewState["status"] = 1;



                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                TextBox_nolist.Text = "";
                Label_EditInformation2.Text = "";
            }
            catch
            {
                Label_result.Text = "输入时间格式不正确";
            }
           
        }

        //删除按钮
        protected void ImageButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            Computer myComputer = new Computer();
            Student myStudent1 = (Student)Session["Student"];
            string str = myStudent1.Computers.Delete(decimal.Parse(Label_ComputerID.Text.ToString()));

            if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
            {
                Label_result.Text = Resources.ErrorMessage.OK;
                Student myStudent2 = (Student)Session["Student"];
                GridView_Computer.DataSource = myStudent2.Computers.GetDataSet();
                GridView_Computer.DataBind();
            }
            else Label_result.Text = Resources.ErrorMessage.DBOptError;

            ViewState["status"] = 1;

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }


        //添加按钮
        protected void ImageButton_Add_Click(object sender, ImageClickEventArgs e)
        {
            Computer myComputer = new Computer();
            Student myStudent1 = (Student)Session["Student"];
            if (TextBox_nolist.Text == null || TextBox_nolist.Text == string.Empty)
                myComputer.computer_level = DropDownList_computerlevel.SelectedValue;
            else
            {
                myComputer.computer_level = TextBox_nolist.Text.ToString();
            }
            try
            {
                myComputer.computer_time = DateTime.Parse(TextBox_computertime.Text.ToString());
                string str = myStudent1.Computers.Add(myComputer);

                if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
                {
                    Label_result.Text = Resources.ErrorMessage.OK;
                    Student myStudent2 = (Student)Session["Student"];
                    GridView_Computer.DataSource = myStudent2.Computers.GetDataSet();
                    GridView_Computer.DataBind();
                }
                else Label_result.Text = Resources.ErrorMessage.DBOptError;

                ViewState["status"] = 1;


                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                TextBox_nolist.Text = "";
                Label_EditInformation2.Text = "";
            }
            catch
            {
                Label_result.Text = "输入时间格式不正确";
            }
                
          
        }

        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            TextBox_computertime.Text = null;
            ViewState["status"] = 1;

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }

        protected void DropDownList_computerlevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList_computerlevel.SelectedValue == "others")
            {
                Label_nolist.Visible = true;
                TextBox_nolist.Visible = true;
                RequiredFieldValidator_nolist.Enabled = true;
            }
            else
            {
                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                RequiredFieldValidator_nolist.Enabled = false;
            }
        }

        
    }
}
